package com.hdy.orderforthree.mvp;

import com.hdy.net.order.NetCallback;
import com.hdy.net.order.bean.DistrictBean;
import com.hdy.net.order.bean.LoginBean;
import com.hdy.net.order.bean.TablesInfoResponse;
import com.hdy.orderforthree.pos.ConcretePosFactory;
import com.hdy.orderforthree.widget.BasePop;
import com.hdy.orderforthree.widget.BillBasePop;
import com.hdy.orderforthree.widget.ConcretePopFactory;
import com.hdy.orderforthree.widget.LoginPop;
import com.hdy.orderforthree.widget.PeopleNumberDialogPop;
import com.hdy.orderforthree.widget.ResetLoginDialogPop;
import com.hdy.orderforthree.widget.TablesPop;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TitleBarPresenter implements ITitleBarContract.IPresenter{
    //布局不复杂，暂时不启用
    private TitleBarModel mTitleBarModel;
    private TitleBarView mTitleBarView;

    private FoodPresenter mFoodPresenter;
    private BillPresenter mBillPresenter;

    /**
     * map中的pop，每次只可以出现一个
     */
    private Map<Integer, BasePop> mBasePops ;

    private BasePop mPeopleNumberDialogPop;
    private BasePop mLoginPop,mResetLogin;
    private BasePop mTablesPop;

    public TitleBarPresenter(TitleBarView view, TitleBarModel model) {
        mTitleBarView = view;
        mTitleBarModel = model;

        mTitleBarView.setPresenterCallBack(new ISwitchView() {
            @Override
            public void showOrder() {
                loadOrderPop();
            }

            @Override
            public void showBill() {
                loadBillPop();
            }

            @Override
            public void addPeopleNumber(boolean isVisible) {
                loadPeopleNumberDialog(isVisible);
            }

            @Override
            public void addLogin() {
                loadResetLogin(true);
            }
        });

        mBasePops = new HashMap<>();
    }

    private void loadLogin(boolean isVisible) {
        if (mLoginPop == null) {
            mLoginPop = new LoginPop(mTitleBarView.getView().getContext());
            mLoginPop.setNetCallback(new NetCallback<TablesInfoResponse>() {
                @Override
                public void onStart() {

                }

                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(TablesInfoResponse o) {
                    if (o.isSuccess()) {
                        mTitleBarView.getView().post(new Runnable() {
                            @Override
                            public void run() {
                                start(ConcretePopFactory.getInstance().getCurrentPop());
                                loadLogin(false);
                                initTablesPop(o.getTables());
                                loadTablesPop(true);
                            }
                        });
                    }
                }
            });
        }
        if (isVisible) {
            mLoginPop.addView();
        } else {
            mLoginPop.removeView();
        }
    }

    private void loadResetLogin(boolean isVisible) {
        if (mResetLogin == null) {
            mResetLogin = new ResetLoginDialogPop(mTitleBarView.getView().getContext());
            mResetLogin.setNetCallback(new NetCallback<TablesInfoResponse>() {
                @Override
                public void onStart() {

                }

                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(TablesInfoResponse o) {
                    if (o.isSuccess()) {
                        mTitleBarView.getView().post(new Runnable() {
                            @Override
                            public void run() {
                                loadResetLogin(false);
                                initTablesPop(o.getTables());
                                loadTablesPop(true);
                            }
                        });
                    }
                }
            });
        }
        if (isVisible) {
            mResetLogin.addView();
        } else {
            mResetLogin.removeView();
        }
    }

    private void addNewPop(int type) {
        //移除其他的pop
        for (int key : mBasePops.keySet()) {
            mBasePops.get(key).removeView();
        }

        //加载新pop
        BasePop needAddPop = mBasePops.get(type);
        if (needAddPop == null) {
            BasePop pop = ConcretePopFactory.getInstance().CreatePop(type, mTitleBarView.getView().getContext());
            needAddPop = pop;
            mBasePops.put(type, pop);
            if (pop instanceof BillBasePop) {
                pop.setUpdateViewCallback(new UpdateViewCallback() {
                    @Override
                    public void showPop(int type) {
                        loadPopLayout(type);
                    }

                    @Override
                    public void showTotalOrderNumberCorner() {

                    }

                    @Override
                    public void showPeopleNumber() {

                    }

                    @Override
                    public void showTableInfo() {

                    }
                });
            }
        }
        needAddPop.addView();
    }

    private void loadOrderPop() {
        addNewPop(ConcretePopFactory.ORDER_POP);
        mTitleBarView.showOrder();

        if (mFoodPresenter == null) {
            mFoodPresenter = new FoodPresenter(new FoodView(mBasePops.get(ConcretePopFactory.ORDER_POP).getView()), new FoodModel());
            mFoodPresenter.loadFoodData();
            mFoodPresenter.setUpdateViewCallback(new UpdateViewCallback() {
                @Override
                public void showPop(int type) {

                }

                @Override
                public void showTotalOrderNumberCorner() {
                    loadTotalOrderNumberCorner();
                }

                @Override
                public void showPeopleNumber() {

                }

                @Override
                public void showTableInfo() {

                }
            });
        }
        mFoodPresenter.notifyDataSetChanged();
    }

    private void loadBillPop() {
        addNewPop(ConcretePopFactory.MY_BILL_POP);
        mTitleBarView.showBill();

        if (mBillPresenter == null) {
            mBillPresenter = new BillPresenter(new BillView(mBasePops.get(ConcretePopFactory.MY_BILL_POP).getView()), new BillModel());
            mBillPresenter.setUpdateViewCallback(new UpdateViewCallback() {
                @Override
                public void showPop(int type) {

                }

                @Override
                public void showTotalOrderNumberCorner() {
                    loadTotalOrderNumberCorner();
                }

                @Override
                public void showPeopleNumber() {

                }

                @Override
                public void showTableInfo() {

                }
            });
        }
        mBillPresenter.loadBillData();
    }

    private void loadPeopleNumberDialog(boolean isVisible) {
        if (mPeopleNumberDialogPop == null) {
            mPeopleNumberDialogPop = new PeopleNumberDialogPop(mTitleBarView.getView().getContext());
            mPeopleNumberDialogPop.setUpdateViewCallback(new UpdateViewCallback() {
                @Override
                public void showPop(int type) {

                }

                @Override
                public void showTotalOrderNumberCorner() {

                }

                @Override
                public void showPeopleNumber() {
                    loadNumberOfPeople();
                }

                @Override
                public void showTableInfo() {

                }
            });
        }
        if (isVisible) {
            mPeopleNumberDialogPop.addView();
        } else {
            mPeopleNumberDialogPop.removeView();
        }
    }

    private void initTablesPop(List<DistrictBean> list) {
        if (mTablesPop == null) {
            mTablesPop = new TablesPop(mTitleBarView.getView().getContext());
            ((TablesPop)mTablesPop).setAdapter(list);
            mTablesPop.setUpdateViewCallback(new UpdateViewCallback() {
                @Override
                public void showPop(int type) {

                }

                @Override
                public void showTotalOrderNumberCorner() {

                }

                @Override
                public void showPeopleNumber() {

                }

                @Override
                public void showTableInfo() {
                    loadTableInfo();
                }
            });
        }
    }

    private void loadTablesPop(boolean isVisible) {
        if (isVisible) {
            mTablesPop.addView();
        } else {
            mTablesPop.removeView();
        }
    }

    public void login() {
        LoginBean loginBean = ConcretePosFactory.getInstance().getPos().getLoginBean();
        if (loginBean == null
                || loginBean.getTables() == null
                || loginBean.getTables().getName() == null
                || loginBean.getTables().getName().length() == 0) {
            loadLogin(true);
        } else {
            start(ConcretePopFactory.getInstance().getCurrentPop());
        }
    }

    public void start(int type) {
        loadPopLayout(type);
        loadTableInfo();
        loadNumberOfPeople();
    }

    @Override
    public void loadPopLayout(int type) {
        if (isViewAttached()) {
            switch (type) {
                case ConcretePopFactory.ORDER_POP:
                    loadOrderPop();
                    break;
                case ConcretePopFactory.MY_BILL_POP:
                    loadBillPop();
                    break;
                default:
                    loadOrderPop();
                    break;
            }
        }
    }

    @Override
    public void loadTotalOrderNumberCorner() {
        if (isViewAttached()) {
            mTitleBarView.showTotalOrderNumberCorner();
        }
    }

    @Override
    public void loadTableInfo() {
        if (isViewAttached()) {
            mTitleBarView.showTableInfo();
        }
    }

    @Override
    public void loadNumberOfPeople() {
        if (isViewAttached()) {
            mTitleBarView.showNumberOfPeople();
        }
    }

    @Override
    public void detachView() {
        mTitleBarView = null;
    }

    @Override
    public boolean isViewAttached() {
        return mTitleBarView!= null;
    }

    public interface ISwitchView {
        void showOrder();
        void showBill();
        void addPeopleNumber(boolean isVisible);
        void addLogin();
    }

    public interface UpdateViewCallback {
        void showPop(int type);
        void showTotalOrderNumberCorner();
        void showPeopleNumber();
        void showTableInfo();
    }

}
